/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.jfonia.view.main.header;

import java.awt.Toolkit;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
import javax.swing.text.PlainDocument;
import org.jfonia.view.main.JTextField;

/**
 *
 * @author Rik Bauwens
 */
public class IntegerTextField extends JTextField
{
        private int maxChar;

        public IntegerTextField(int charLimit)
        {
                this(null, charLimit, charLimit);
        }

        public IntegerTextField(int columns, int charLimit)
        {
                this(null, columns, charLimit);
        }

        public IntegerTextField(String string, int columns, int charLimit)
        {
                super(columns);
                setHorizontalAlignment(JTextField.RIGHT);
                maxChar = charLimit;
                setText(string);
        }

        /**
        * Returns the integer value of the text field or null in case of a number
        * format exception... like on a empty text field.
        * @return
        */
        public Integer getIntegerValue()
        {
                String text = getText();
                try
                {
                        Integer integer = new Integer( text );
                        return integer;
                }
                catch ( NumberFormatException exp )
                {
                        return null;
                }
        }

        @Override
        protected Document createDefaultModel()
        {
                return new IntegerDocument();
        }

        class IntegerDocument extends PlainDocument
        {
                IntegerDocument()
                {
                        super();
                }

                @Override
                public void insertString(int offset, String str, AttributeSet a) throws BadLocationException
                {
                        if (str == null)
                        {
                                return;
                        }

                        // check size limit...
                        int tomuch = str.length() + getLength() - maxChar;
                        if (tomuch > 0)
                        {
                                str = str.substring(0, str.length() - tomuch);
                                Toolkit.getDefaultToolkit().beep();
                        }

                        // check if digit..
                        char[] orgCharArr = str.toCharArray();
                        char[] addCharArr = new char[ orgCharArr.length ];
                        int j = 0;
                        for ( int i = 0; i < orgCharArr.length; i++ )
                        {
                                if ( Character.isDigit( orgCharArr[i] ) )
                                {
                                        addCharArr[ j ] = orgCharArr[i];
                                        j++;
                                }
                        }

                        super.insertString(offset, new String( addCharArr, 0, j ), a);
                }
        }
}
